# //循环随机播放多条视频，先播放第一个，后面视频随机播放

# 1.run path1&path2&path3... random

# //循环顺序播放多条视频

# 2.run path1&path2&path3... list

# //播放单条视频,仅播放一次

# 3.run path one
import asyncio
from typing import Any
import websockets
import threading
import cv2
import pyvirtualcam
import time
import re
import colorsys
import numpy as np
import random

class VideoPlay:
    """视频播放类"""
    def __init__(self,_pathlist,_playtype):
        """
        _pathlist:视频路径数组
        _playtype:播放类型one-播放一次 random-随机循环播放 list-顺序循环播放
        """
        self.pathlist = _pathlist
        """播放列表，存放播放路径"""
        self.playtype = _playtype
        """播放类型one-播放一次 random-随机循环播放 list-顺序循环播放"""
        self.playing = ""
        """当前播放的视频"""
        self.IsPlaying = False
        """播放状态"""
        self.cam = None
    def playvideo(self):
        self.IsPlaying = True
        # 初始化当前播放次数
        playtime = 1
        # //初始化摄像头
        videopath = self.pathlist[0]
        video = cv2.VideoCapture(videopath)
        width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
        height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
        fps = video.get(cv2.CAP_PROP_FPS)
        print(width,height,fps)
        video.release()
        self.cam = pyvirtualcam.Camera(width=width, height=height, fps=fps)

        while self.IsPlaying:
            if playtime != 1:
                if self.playtype == "one": 
                    break
                if self.playtype == "random":
                    # 随机获取一条除上次播放之外的视频
                    nextvedio = self.get_random_different_item(self.pathlist,self.playing)
                    self.playing = nextvedio
                if self.playtype == "list":
                    # 获取列表中下一条视频
                    nextvedio = self.get_next_element(self.pathlist,self.playing)
                    self.playing = nextvedio
            else:
                self.playing = self.pathlist[0]
            video = cv2.VideoCapture(self.playing)
            print("第"+str(playtime)+"次，开始播放："+self.playing)
            while self.IsPlaying:
                grabbed, frame = video.read()
                if not grabbed:
                    print('当前视频播放结束')
                    break
                frame = cv2.resize(frame,(self.cam.width,self.cam.height))
                frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                self.cam.send(frame_rgb)
                self.cam.sleep_until_next_frame()
            video.release()
            playtime+=1
        self.cam.close()
    def get_next_element(self,string_list, target_string):
        """ 获取字符串在列表中的下一个元素，如果是最后一个则返回第一个
        """
        index = string_list.index(target_string)
        next_index = (index + 1) % len(string_list)
        return string_list[next_index]
    def get_random_different_item(self,string_list, current_item):
        """从列表中获取一个非当前项目的随机项目
        """
        # 从列表中排除当前项目
        other_items = [item for item in string_list if item != current_item]
        # 随机选择一个项目
        random_item = random.choice(other_items)
        return random_item
    def stopplay(self):
        self.IsPlaying = False
vdolst = "C:\\Users\\14794\\Videos\\测试推送视频素材\\01.mp4","C:\\Users\\14794\\Videos\\测试推送视频素材\\02.mp4","C:\\Users\\14794\\Videos\\测试推送视频素材\\03.mp4"
# ,"C:\\Users\\14794\\Videos\\测试推送视频素材\\04.mp4"
pltp = "random"
vp = VideoPlay(vdolst,pltp)
vp.playvideo()
